import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/afterSales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '技术升级单'
});

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const getUpdateDesc = formatMessage({
    id: 'api.getUpdate',
    defaultMessage: '修改{resourceName}'
}, {resourceName});

const getSubmitDesc = formatMessage({
    id: 'api.getSubmitDesc',
    defaultMessage: '提交'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});

const getPublishDesc = formatMessage({
    id: 'api.getPublishDesc',
    defaultMessage: '发布'
});

const getCloseDesc = formatMessage({
    id: 'api.getCloseDesc',
    defaultMessage: '关闭'
});

const getPassDesc = formatMessage({
    id: 'api.getPassDesc',
    defaultMessage: '审核通过'
});

const getRejectDesc = formatMessage({
    id: 'api.getRejectDesc',
    defaultMessage: '审核不通过'
});

const getRejectToPrevDesc = formatMessage({
    id: 'api.getRejectToPrevDesc',
    defaultMessage: '驳回'
});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});

const getDutyUnitsDesc = formatMessage({
    id: 'api.getDutyUnitsDesc',
    defaultMessage: '责任单位查询'
});

const partQueryDesc = formatMessage({
    id: 'api.partQueryDesc',
    defaultMessage: '查询备件信息'
});

const fuliaoPartQueryDesc = formatMessage({
    id: 'api.fuliaoPartQueryDesc',
    defaultMessage: '查询辅料信息'
});

const productCategoryDesc = formatMessage({
    id: 'api.productCategoryDesc',
    defaultMessage: '查询产品分类'
});

const vehicleDesc = formatMessage({
    id: 'api.vehicleDesc',
    defaultMessage: '查询车辆'
});

// 初始化请求
export const getInitData = () =>
    request(`${baseUrl}/ui/technologyActivities/init`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询技术升级单
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/technologyActivities?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 新增技术升级单
export const newOrder = data => request(`${baseUrl}/technologyActivities`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 查询技术升级单详情
export const getDetail = id => request(`${baseUrl}/technologyActivities/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 修改技术升级单
export const updateOrder = (id, data) => request(`${baseUrl}/technologyActivities/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getUpdateDesc));

// 提交
export const submitData = (id, data) => request(`${baseUrl}/technologyActivities/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSubmitDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// 发布
export const publishData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/publish`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getPublishDesc));

// 关闭
export const closeData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/close`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getCloseDesc));

// 审核通过
export const passData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getPassDesc));

// 审核不通过
export const rejectData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getRejectDesc));

// 驳回
export const rejectToPrevData = (id, rowVersion) => request(`${baseUrl}/technologyActivities/${id}/rejectToPrev`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getRejectToPrevDesc));

// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/technologyActivities/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

//VIN导入模板
export const importTemp = () => request(`${baseUrl}/technologyActivities/vinImportTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

// 快捷查询责任单位
export const getDutyUnitsQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits/base/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitsDesc));
};
// 查询责任单位
export const getDutyUnits = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits/base?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitsDesc));
};

//服务站备件查询
export const partQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};

//辅料查询
export const fuliaoPartQuery = data => request(`${baseUrl}/parts/query`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, fuliaoPartQueryDesc));

// 查询产品分类
export const getProductCategories = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryDesc));
};

// 查询车辆
export const getVehicleData = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/technologyActivities/vehicle?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, vehicleDesc));
};
